Sharpen Your Skills: Programming Languages Practice Q&A

Java if else, if else ladder and switch Questions

 
1

Question- Write a program for checking enter number is even or odd.



Answer-
  package csdtpatna;
  import java.util.Scanner;
  class EvenOdd
  {
  public static void main(String []args)
 {
//Creating object of Scanner class for taking input from User.
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter a number");
 int n=sc.nextInt();
 if(n%2==0)
 {
 System.out.println("Number is Even");
 }
 else
 {
 System.out.println("Number is Odd");
 }

 }

 }
2

Question- Write a program to take two number from user and check who is max.



Answer-
package csdtpatna;
import java.util.Scanner;
class MaxMin
{
public static void main(String []args)
{
//Creating object of Scanner classfor taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter two number");
int n1=sc.nextInt();
int n2=sc.nextInt();
if(n1==n2)
{
System.out.println("Both Number is Equal:: n1="+n1+" And n2="+n2);
}
else if(n1>n2)
{
System.out.println("Number N1 is Max::"+n1);
}
else
{
System.out.println("Number N2 is Max::"+n2);
}

}

}
3

Question- Check whether an alphabet is vowel or consonant using if..else statement.



Answer-
 public class VowelConsonant 
 {
    public static void main(String[] args) {
        char ch = 'i';
        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
            System.out.println(ch + " is vowel");
        else
            System.out.println(ch + " is consonant");
    }
  }
4

Question- Write a program for checking enter number is Positive or Negative.



Answer-

package csdtpatna;
import java.util.Scanner;
class NegativePositive
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
if(n>=0)
{
System.out.println("Number is Positive");
}
else
{
System.out.println("Number is Negative");
}

}

}
5

Question- Write a program to take input age of person and check he is eligible for marriage or not.



Answer-
package csdtpatna;
import java.util.Scanner;
class Marriage
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter age of Person");
int age=sc.nextInt();
if(age>=21)
{
System.out.println("Person is eligible for marriage");
}
else
{
System.out.println("Person is not eligible for marriage");
}

}

}
6

Question- Write a program to take input age of male or Female and check he/she is eligible for marriage or not.



Answer-
package csdtpatna;
import java.util.Scanner;
class Marriage2
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter age of Male or Female");
int age=sc.nextInt();
System.out.println("Enter Gender of Human like male or female");
String gen=sc.nextLine();
if(gen=="male")
{

if(age>=21)
{
System.out.println("Male is eligible for marriage becz age is:"+age);
}
else
{
System.out.println("Male is not  eligible for marriage becz age is:"+age);
}
}

else if(gen=="female")
{
if(age>=18)
{
System.out.println("female is eligible for marriage becz age is:"+age);
}
else
{
System.out.println("female is not  eligible for marriage becz age is:"+age);
}
}
else
{

System.out.println("Not eligible for marriage becz gen is NNN:");
}
}

}
7

Question- Write a program to take input age of person and job status like yes or no and check he is eligible for marriage or not.



Answer-
package csdtpatna;
import java.util.Scanner;
class Marriage3
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter age of Person");
int age=sc.nextInt();
System.out.println("Enter Job stauts like yes or no");
String job=sc.nextLine();
if(job=="yes")
{
System.out.println("Person is eligible for marriage a/c to job status");

if(age>=21)
{
System.out.println("Person is eligible for marriage becz age is:"+age);
}
else
{
System.out.println("Person is not  eligible for marriage becz age is:"+age);
}
}

else
{
System.out.println("Person is not  eligible for marriage becz no job");
}

}

}
8

Question- Write a java program to input a cost of a pen and calculate 70 pens cost with some discount. if total pen cost greater than 1000 then we calculate 20% discount otherwise 10%.



Answer-
package csdtpatna;
import java.util.Scanner;
class PenCost
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter Cost of a Pen");
int one_pen_cost=sc.nextInt();
int TotalCost=70*one_pen_cost;
int dis;
if(TotalCost>=1000)
{
dis=TotalCost*20/100;
System.out.println("Dear customer you Pay only Rs. "+(TotalCost-dis));
}
else
{
dis=TotalCost*10/100;
System.out.println("Dear customer you Pay only Rs. "+(TotalCost-dis));
}
System.out.println("One Pen Cost is:: Rs. "+one_pen_cost);
System.out.println("Total Pen Cost without discount is:: Rs. "+TotalCost);
System.out.println("Total discount Amount is:: Rs. "+dis);
}

}
9

Question- Write a java program to take Basic salary of employee from user and calculate gross salary with given condition, if Basic salary >= 10000 then we calculate 40% da and 30% ta of Basic salary otherwise 30% da and 20% ta of Basic salary.



Answer-
package csdtpatna;
import java.util.Scanner;
class EmployeeSalary
{
public static void main(String []args)
{
//Creating object of Scanner class for taking input from User.
Scanner sc=new Scanner(System.in);
System.out.println("Enter Basic Salary");
int bs=sc.nextInt();
int da,ta,gs;
if(bs>=10000)
{
da=bs*40/100;
ta=bs*30/100;

}
else
{
da=bs*30/100;
ta=bs*20/100;
}
System.out.println("Basic Salary is:: Rs. "+bs);
System.out.println("DA is:: Rs. "+da+" and TA is Rs "+ta);
System.out.println("Gross Salary is:: Rs. "+(bs+da+ta));
}

}
10

Question- Write a java program to Take values of length and breadth of a rectangle from user and check if it is square or not.



Answer-
import java.util.Scanner;
 class Rectangle
  {
  public static void main(String[] args)
  { //Creating object of Scanner Class
    Scanner s = new Scanner(System.in);
    System.out.println("Enter length");
    int x = s.nextInt();
    System.out.println("Enter breadth");
    int y = s.nextInt();
    if(x==y)
    {
      System.out.println("Square");
    }
    else
    {
      System.out.println("Rectangle");
    }
  }
}
11

Question- Write a java program to A shop will give discount of 10% if the cost of purchased quantity is more than 2000. Ask user for quantity, Suppose, one unit will cost 100. Judge and print total cost for user.



Answer-
import java.util.Scanner;
class Shopping{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter quantity");
    int x = s.nextInt();
    int T=x*100;
    if(T>2000)
    {
      System.out.println("You get a discount of "+(.1*T)+" and your total cost is "+(T-(.1*T)));
    }
    else
   {
      System.out.println("No discount, you pay only Rs "+T);
    }
  }
}
12

Question- Write a java program to A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years. Ask user for their salary and year of service and print the net bonus amount.



Answer-
import java.util.Scanner;
class Bonus{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter Current Salary");
    int cs = s.nextInt();
    System.out.println("Enter Year of service");
    int y = s.nextInt();
    
    if(y>5)
    {
      System.out.println("You get a bonus of "+(cs*5/100)+" and your total Salary is "+(cs+cs*5/100)));
    }
    else
   {
      System.out.println("No Bonus, your Salary is "+cs);
    }
  }
}
13

Question- Write a java program to A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A ,Ask user to enter marks and print the corresponding grade.



Answer-
import java.util.Scanner;
 class Grading{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter your marks between 1 to 100");
    int x = s.nextInt();
    if(x<25)
    {
      System.out.println("F");
    }
    else if((x>=25)&&(x<45))
    {
      System.out.println("E");
    }
    else if((x>=45)&&(x<50))
   {
      System.out.println("D");
    }
    else if((x>=50)&&(x<60))
    {
      System.out.println("C");
    }
    else if((x>=60)&&(x<80))
    {
      System.out.println("B");
    }
    else if((x>=80)&&(x<=100))
    {
      System.out.println("A");
    }
    else
    {
      System.out.println("Not correct marks");
    }
  }
}
14

Question- Write a java program to Check enter number is perfect number or not perfect number.



Answer-
import java.io.*;
 import java.util.Scanner;
 import java.lang.Math;
 public class PerfectNumber 
   {
 public static void main(String[] args) 
      { 
  Scanner sc= new Scanner(System.in);
  System.out.println("Enter a number");     
  int n=sc.nextInt();
  int sqrt=(int)Math.sqrt(n);
  if(sqrt*sqrt==n)
 {
  System.out.println("Yes");
 }
  else
 {
    System.out.println("No");
 }
 }
 }

15

Question- Write a program to check Two input numbers are equels or not.



Answer-
import java.io.*;
 import java.util.Scanner;
 public class Test 
 {
 public static void main(String[] args) 
 { 
 Scanner sc= new Scanner(System.in);
 double a,b;
  System.out.println("Enter 1st number");
 a=sc.nextDouble();
 System.out.println("Enter 2nd number");
 b=sc.nextDouble();
 if(a==b)
 {
 System.out.println("They are the same");
  }
  else
  {
    System.out.println("They are different");
   }
  }
 }
16

Question- write a program to check Triangle is valid or not.



Answer-
import java.io.*;
 import java.util.Scanner;
 public class Test
  {
 public static void main(String[] args) 
 {
   Scanner sc= new Scanner(System.in);
 System.out.println("Enter traingle side1");
   int n1=sc.nextInt();
 System.out.println("Enter traingle side2");
   int n2=sc.nextInt();
 System.out.println("Enter traingle side3");
   int n3=sc.nextInt();
   int s=n1+n2+n3;
   if(s==180)
      System.out.println("Traingle is valid");
   else
      System.out.println("Traingle is not valid");
   }
 }
17

Question- Write a program to Take input of age of 3 people by user and determine oldest among them.



Answer-
import java.io.*;
 import java.util.Scanner;
 public class Test 
  {
  public static void main(String[] args) 
    { 
  Scanner sc = new Scanner (System.in);
  System.out.println("Enter age of 1st person");
       int a=sc.nextInt();
 System.out.println("Enter age of 2nd person");
       int b=sc.nextInt();
 System.out.println("Enter age of 3rd person");
       int c=sc.nextInt();
       if((a>b)&&(a>c))
         System.out.println(" 1st Person is Oldest bcz its age is"+a);
       else if(b>c)
         System.out.println(" 2nd Person is Oldest bcz its age is"+b);
       else
   System.out.println(" 3rd Person is Oldest bcz its age is"+c);
      
}
 }
18

Question- Enter age and check eligible for vote or not.



Answer-
import java.io.*;
import java.util.Scanner;
public class Test
  {
  public static void main(String[] args) 
    { 
  Scanner sc = new Scanner (System.in);
 System.out.println("Enter age");
       int a=sc.nextInt();
       if(a<18)
         System.out.println("You are not eligible to vote");
       else
         System.out.println("You are eligible to vote");
      
}
 }
19

Question- Write a program to print absolute vlaue of a number entered by user. E.g.- INPUT: 1 OUTPUT: 1 INPUT: -1 OUTPUT: 1



Answer-
class Test{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter number");
    int x = s.nextInt();
    if(x<0){
      System.out.println("Absolute value : "+(-1*x));
    }
    else{
      System.out.println("Absolute value : "+x);
    }
  }
 }
20

Question- Write a Java program to solve quadratic equations.



Answer-
import java.util.Scanner;
public class Exercise2 {

    
  public static void main(String[] Strings)
{
 Scanner input = new Scanner(System.in);

  System.out.print("Input a: ");
  double a = input.nextDouble();
  System.out.print("Input b: ");
  double b = input.nextDouble();
  System.out.print("Input c: ");
  double c = input.nextDouble();

  double result = b * b - 4.0 * a * c;

   if (result > 0.0) {
     double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
     double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
     System.out.println("The roots are " + r1 + " and " + r2);
            }
      else if (result == 0.0) {
        double r1 = -b / (2.0 * a);
        System.out.println("The root is " + r1);
            }
      else {
  System.out.println("The equation has no real roots.");
       }
    }
}
21

Question- Write a Java program that takes a year from user and print whether that year is a leap year or not. Input the year: 2016 Expected Output : 2016 is a leap year



Answer-
import java.util.Scanner;
public class Exercise9 {

    
  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Input the year: ");
        int year = in.nextInt();

        boolean x = (year % 4) == 0;
        boolean y = (year % 100) != 0;
        boolean z = ((year % 100 == 0) && (year % 400 == 0));

        if (x && (y || z))
        {
            System.out.println(year + " is a leap year");
        }
        else
        {
            System.out.println(year + " is not a leap year");
        }
    }
}
22

Question- Check whether an alphabet is vowel or consonant using switch statement.



Answer-
public class VowelConsonant {
    public static void main(String[] args) {
        char ch = 'z';
        switch (ch) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                System.out.println(ch + " is vowel");
                break;
            default:
                System.out.println(ch + " is consonant");
        }
    }
}
23

Question- Write a Java program to find the number of days in a month.



Answer-
import java.util.Scanner;
public class Exercise7 {

    
  public static void main(String[] strings) {

        Scanner input = new Scanner(System.in);

        int number_Of_DaysInMonth = 0; 
        String MonthOfName = "Unknown";

        System.out.print("Input a month number: ");
        int month = input.nextInt();

        System.out.print("Input a year: ");
        int year = input.nextInt();

        switch (month) {
            case 1:
                MonthOfName = "January";
                number_Of_DaysInMonth = 31;
                break;
            case 2:
                MonthOfName = "February";
                if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
                    number_Of_DaysInMonth = 29;
                } else {
                    number_Of_DaysInMonth = 28;
                }
                break;
            case 3:
                MonthOfName = "March";
                number_Of_DaysInMonth = 31;
                break;
            case 4:
                MonthOfName = "April";
                number_Of_DaysInMonth = 30;
                break;
            case 5:
                MonthOfName = "May";
                number_Of_DaysInMonth = 31;
                break;
            case 6:
                MonthOfName = "June";
                number_Of_DaysInMonth = 30;
                break;
            case 7:
                MonthOfName = "July";
                number_Of_DaysInMonth = 31;
                break;
            case 8:
                MonthOfName = "August";
                number_Of_DaysInMonth = 31;
                break;
            case 9:
                MonthOfName = "September";
                number_Of_DaysInMonth = 30;
                break;
            case 10:
                MonthOfName = "October";
                number_Of_DaysInMonth = 31;
                break;
            case 11:
                MonthOfName = "November";
                number_Of_DaysInMonth = 30;
                break;
            case 12:
                MonthOfName = "December";
                number_Of_DaysInMonth = 31;
        }
        System.out.print(MonthOfName + " " + year + " has " + number_Of_DaysInMonth + " days\n");
    }
}

Copy
Sample Output:

Input a month number: 2                                                                                       
Input a year: 2016                                                                                            
February 2016 has 29 days

24

Question- Write a Java program to take two Input number is equal or not.



Answer-
import java.io.*;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) { 
       Scanner sc = new Scanner(System.in);
         double a,b;
         a = sc.nextDouble();
         b = sc.nextDouble();
        if(a == b)
        {
          System.out.println("They are the same");
        }
       else
       {
         System.out.println("They are different");
       }
}
}